/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.mp.common.platform.controller;

import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.mp.common.model.WeixinSubscribe;
import com.yami.shop.mp.common.service.WeixinSubscribeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2019/01/16.
 */
@RestController
@RequestMapping("/weixin/subscribe")
@Api(tags = "平台端微信关注回复接口")
public class WeixinSubscribeController {

    @Autowired
    private WeixinSubscribeService weixinSubscribeService;

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('weixin:subscribe:page')")
    @ApiOperation(value = "分页获取微信关注回复数据")
    public ServerResponseEntity<List<WeixinSubscribe>> page(){
        List<WeixinSubscribe> list =  weixinSubscribeService.listWeixinSubscribeAndMaterial();
        for (WeixinSubscribe weixinSubscribe : list) {
            weixinSubscribe.setContent(weixinSubscribe.getContent());
        }
        return ServerResponseEntity.success(list);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('weixin:subscribe:save')")
    @ApiOperation(value = "保存微信关注回复数据")
    public ServerResponseEntity<Void> save(@RequestBody @Valid WeixinSubscribe weixinSubscribe){
        weixinSubscribe.setContent(weixinSubscribe.getContent());
        weixinSubscribe.setCreateTime(new Date());
        weixinSubscribeService.save(weixinSubscribe);
        weixinSubscribeService.removeWeixinSubscribeAndMaterial();
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('weixin:subscribe:update')")
    @ApiOperation(value = "修改微信关注回复数据")
    public ServerResponseEntity<Void> update(@RequestBody @Valid WeixinSubscribe weixinSubscribe){
        weixinSubscribe.setContent(weixinSubscribe.getContent());
        weixinSubscribeService.updateById(weixinSubscribe);
        weixinSubscribeService.removeWeixinSubscribeAndMaterial();
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('weixin:subscribe:delete')")
    @ApiOperation(value = "通过id删除微信关注回复数据")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    public ServerResponseEntity<Void> delete(@PathVariable Long id){
        weixinSubscribeService.removeById(id);
        weixinSubscribeService.removeWeixinSubscribeAndMaterial();
        return ServerResponseEntity.success();
    }
}
